using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
/* using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame; */
using UnityEngine;
using UnityEngine.SocialPlatforms;

public class GPGS : MonoBehaviour
{
	public static GPGS instance;

	public string leaderBoardID;

	public List<string> myAchievementsList = new List<string>();

	[HideInInspector]
	public string saveString;

	[HideInInspector]
	public double[] MyAchievments;

	[HideInInspector]
	public bool AddScoreToLeaderBoardSuccessedSwitch;

	[HideInInspector]
	public bool GetMyScoreSuccessedSwitch;

	public bool GPGSLogin { get; set; }

	public bool isSaving { get; set; }

	public bool connecting { get; set; }

	private void Awake()
	{
		if (instance != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	private void Start()
	{
		if (PlayerPrefs.GetInt("NoGPGS") == 0)
		{
			Init();
			LoginGPGS();
		}
	}

	public void Init()
	{
		GPGSLogin = false;
		/* PlayGamesClientConfiguration configuration = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
		PlayGamesPlatform.InitializeInstance(configuration);
		PlayGamesPlatform.DebugLogEnabled = false;
		PlayGamesPlatform.Activate(); */
	}

	public void SaveCloud(bool saveSwitch)
	{
		connecting = true;
		if (Social.localUser.authenticated)
		{
			isSaving = saveSwitch;
			//((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("StickmanTheFlashSave", DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpened);
		}
		else
		{
			connecting = false;
		}
	}

	/* private void SavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game)
	{
		if (status == SavedGameRequestStatus.Success)
		{
			if (isSaving)
			{
				byte[] updatedBinaryData = ToBytes(PlayerPrefs.GetInt("playerGem").ToString());
				SavedGameMetadataUpdate updateForMetadata = default(SavedGameMetadataUpdate.Builder).WithUpdatedDescription("Saved Game at " + DateTime.Now).Build();
				((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, updateForMetadata, updatedBinaryData, SavedGameWritten);
				DebugText.instance.Show("Cloud SAVED");
			}
			else
			{
				((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, SavedGameLoaded);
			}
		}
		else
		{
			DebugText.instance.Show("Error opening game" + status);
		}
	} */

	/* private void SavedGameLoaded(SavedGameRequestStatus status, byte[] data)
	{
		connecting = false;
		if (status == SavedGameRequestStatus.Success)
		{
			LoadFromString(Encoding.ASCII.GetString(data));
		}
		else
		{
			DebugText.instance.Show("Error Reading game: " + status);
		}
	} */

	/* private void SavedGameWritten(SavedGameRequestStatus status, ISavedGameMetadata game)
	{
		connecting = false;
		DebugText.instance.Show("save: " + status);
	} */

	private void LoadFromString(string cloudData)
	{
		DebugText.instance.Show("Loading Success: " + cloudData);
		int num = int.Parse(cloudData);
		if (PlayerPrefs.GetInt("playerGem") < num)
		{
			PlayerPrefs.SetInt("playerGem", num);
		}
	}

	public void LoginGPGS()
	{
		/* if (!Social.localUser.authenticated)
		{
			StartCoroutine(StartLogin());
		} */
	}

	/* private IEnumerator StartLogin()
	{
		DebugText.instance.Show("로그인 시도");
		yield return new WaitForSeconds(0f);
		Social.localUser.Authenticate(LoginCallBackGPGS);
	} */

	public void LoginCallBackGPGS(bool result)
	{
		/* GPGSLogin = result;
		if (GPGSLogin)
		{
			DebugText.instance.Show("로그인 성공");
			PlayerPrefs.SetInt("NoGPGS", 0);
			SaveCloud(false);
			GetMyHighScore();
		}
		else
		{
			DebugText.instance.Show("로그인 실패");
			PlayerPrefs.SetInt("NoGPGS", 1);
		} */
	}

	public void LogoutGPGS()
	{
		/* if (Social.localUser.authenticated)
		{
			DebugText.instance.Show("로그아웃");
			((PlayGamesPlatform)Social.Active).SignOut();
			GPGSLogin = false;
		} */
	}

	public Texture2D GetImageGPGS()
	{
		if (Social.localUser.authenticated)
		{
			return Social.localUser.image;
		}
		return null;
	}

	public string GetNameGPGS()
	{
		if (Social.localUser.authenticated)
		{
			return Social.localUser.userName;
		}
		return null;
	}

	public string GetIdGPGS()
	{
		if (Social.localUser.authenticated)
		{
			return Social.localUser.id;
		}
		return null;
	}

	public void AddScoreToLeaderBoard(int score)
	{
		if (!Social.localUser.authenticated)
		{
			return;
		}
		Social.ReportScore(score, leaderBoardID, delegate (bool success)
		{
			if (success)
			{
				//DebugText.instance.Show("리더보드 기록 성공");
				AddScoreToLeaderBoardSuccessedSwitch = true;
			}
			else
			{
				//DebugText.instance.Show("리더보드 기록 실패");
			}
		});
	}

	public void GetMyHighScore()
	{
		//PlayGamesPlatform.Instance.LoadScores(leaderBoardID, LeaderboardStart.PlayerCentered, 1, LeaderboardCollection.Public, LeaderboardTimeSpan.AllTime, GetMyBestScoreCallback);
	}

	/* private void GetMyBestScoreCallback(LeaderboardScoreData data)
	{
		int num = int.Parse(data.PlayerScore.formattedValue);
		if (PlayerPrefs.GetInt("highestScore") < num)
		{
			PlayerPrefs.SetInt("highestScore", int.Parse(data.PlayerScore.formattedValue));
		}
	} */

	public void ShowResultMyRank()
	{
		//PlayGamesPlatform.Instance.LoadScores(leaderBoardID, LeaderboardStart.PlayerCentered, 1, LeaderboardCollection.Public, LeaderboardTimeSpan.AllTime, ShowResultMyRankCallback);
	}

	/* private void ShowResultMyRankCallback(LeaderboardScoreData data)
	{
		if (data != null)
		{
			GetMyScoreSuccessedSwitch = true;
		}
	} */

	private void ShowRankUpDownArrow(int pastRank, int currentRank)
	{
	}

	public void DayOfStarted()
	{
		/* ((PlayGamesLocalUser)Social.localUser).GetStats(delegate(CommonStatusCodes rc, PlayerStats stats)
		{
			if (rc <= CommonStatusCodes.Success && !stats.HasDaysSinceLastPlayed())
			{
			}
		}); */
	}

	public void GetMyAchievements()
	{
		Social.LoadAchievements(delegate (IAchievement[] achievements)
		{
			for (int i = 1; i < MyAchievments.Length; i++)
			{
				for (int j = 0; j < achievements.Length; j++)
				{
					if (achievements[j].id == myAchievementsList[i])
					{
						MyAchievments[i] = achievements[j].percentCompleted;
						break;
					}
				}
			}
		});
	}

	public void CompletedTheAchievement(int unlockingVehicleNumber)
	{
		string achievementID = myAchievementsList[unlockingVehicleNumber];
		Social.ReportProgress(achievementID, 100.0, delegate
		{
		});
	}

	public void ShowLeaderboard()
	{
		if (!GPGSLogin)
		{
			Init();
			LoginGPGS();
		}
		else
		{
			DebugText.instance.Show("리더보드 출력시도");
			//((PlayGamesPlatform)Social.Active).ShowLeaderboardUI(leaderBoardID);
		}
	}

	public void ShowAchievementsUI()
	{
		Social.ShowAchievementsUI();
	}

	private byte[] ToBytes(string str)
	{
		return Encoding.UTF8.GetBytes(str);
	}

	private string FromBytes(byte[] bytes)
	{
		return Encoding.UTF8.GetString(bytes);
	}
}
